var tabbox = Class.create({
  initialize: function(name, type, section, holder, form) {
	this.holder = holder;
  	this.form = form;
    this.name = name;
    this.type = type;
    this.section = section?section:'';
    

  },
  
  clear: true,
  
  current: "",
  
  select: function(id) {
	 // alert('juff'+ this.id);
  	this.id = id;
  	var target = $(this.name + "-tab-"+id);
  	this.deselect();
  	target.addClassName('selected');
  	
  	if (this.clear == true){
	  	if (this.current){
	  		var old = document.getElementById(this.current);
	  		this.remove(old);
	  	};
	$(this.name+'-content').setStyle({ 'background-image': 'url(images/global/assets/loading.gif)' });
  	}

  	this.apiCall(id);
  },
  
  remove: function(element){
	new Effect.Fade(element, { duration: 0.5, afterFinish:this.destory.bindAsEventListener(this) });
  },
  
  destory: function(target){
  	var old = target.element.parentNode;
  	old.removeChild(target.element);
  },
  
  deselect: function (){
  	var target = document.getElementById(this.name + "-tabs");
  	var tabs = target.getElementsByTagName("li");
  	for (var i = 0; i < tabs.length; i++){
  		$(tabs[i]).removeClassName('selected');
  	};
  },
    
  apiCall: function(params){
	 //alert(this.id);
    var options = { method : 'post', parameters : 'r=feed/' + this.type + '&section='+this.section, onSuccess: this.onComplete.bindAsEventListener(this) };
    new Ajax.Request(Loader.AJAX_PATH + 'index.php', options);
  },
  

  
  onComplete: function(transport) {
	var element = this.name+"-content";
	var target = $(element);
  	target.style.backgroundImage = "none";
    target.innerHTML = transport.responseText;
    var tween = document.getElementById(this.id+"-tween");
    tween.style.display = "none";
    Effect.Appear(tween, { duration: 0.5 });
  }

});


function CallUrl(){
	
	if(this.id == 'news'){
		var options = { method : 'post', parameters : 'r=feed/hotnews' + '&section='+this.section, onSuccess: this.onComplete.bindAsEventListener(this) };
	}
	else { 
		var options = { method : 'post', parameters : 'r=feed/hotarticle' + '&section='+this.section, onSuccess: this.onComplete.bindAsEventListener(this) };
	}
	new Ajax.Request(Loader.AJAX_PATH + 'index.php', options);
  }
  
  
function personTabSelect(id, page) {
	
  	this.id = id;
  	if(!page) {
	  	var target = $(this.name + "-tab-"+id);
		this.deselect();
	  	target.addClassName('selected');
  	}
  	if (this.clear == true){
	  	if (this.current){
	  		var old = document.getElementById(this.current);
	  		this.remove(old);
	  	};
	  	$(this.name+'-box').setStyle({ 'background-image': 'url(images/global/assets/loading.gif)' });
  	}  
  	this.apiCall(id, page);
  	
 }

function personTabDeselect(){	
  	var target = document.getElementById("search-controls");
  	var tabs = target.getElementsByTagName("li");
  	for (var i = 0; i < tabs.length; i++){
  		$(tabs[i]).removeClassName('selected');
  	};  
}

function personTabApiCall(params, page){
	if(page == null) page = 1;
    var options = { method : 'post', parameters : 'r=feed/' + this.type + '&contentType='+ params + '&page='+page, onSuccess: this.onComplete.bindAsEventListener(this) };
    new Ajax.Request(Loader.AJAX_PATH + 'index.php', options);
}

/* Operate after the search function complete */
function personTabComplete(transport){
	var data = eval('(' + transport.responseText + ')');
	
	var element = this.name+"-results";
	var target = $(element);
	target.style.backgroundImage = "none";
	target.innerHTML = data.content;  
	
	var pnElement = this.name+"-links-top";
	var pnTarget = $(pnElement);
	pnTarget.style.backgroundImage = "none";
	pnTarget.innerHTML = data.pageNav; 
	
	var pnElement2 = this.name+"-links-bottom";
	var pnTarget2 = $(pnElement2);
	pnTarget2.style.backgroundImage = "none";
	pnTarget2.innerHTML = data.pageNav; 
	
	var titles = document.getElementsByClassName('title');
	for(var i=0; i<titles.length; i++ ){
		titles[i].style.color = "#336600";
	}
	
}

function newsComplete(transport){
	var target = $(this.name+"-content");
  	target.setStyle({ 'background-image': 'none' });
  	var json = transport.responseJSON;
  	var tmp_ul = new Element("ul");
  	for (var i = 0; i < json.length; i++){
  		var tmp_a = new Element('a', { href: json.items[i].link ,target:'_blank'}).update(json.items[i].title);
  		var tmp_li = new Element('li').update(tmp_a);
  		if (i == (json.length-1) ) tmp_li.addClassName('end');
  		tmp_ul.appendChild(tmp_li);
  	};
  	var id = this.name + "-content-" + json.action;
  	var tmp_div = new Element('div', { 'class': 'tabbox-content-holder', 'id': id }).update(tmp_ul);
  	tmp_div.setStyle({ 'display': 'none'  });
  	target.appendChild(tmp_div);
	Effect.Appear(tmp_div, { duration: 0.5 });
  	
  	this.current = id;
}

/* GALLERY BOX */
function galleryComplete(transport){
	var target = $(this.name+"-content");
  	target.setStyle({ 'background-image': 'none' });
  	var json = transport.responseJSON;
  	
	gallery =  new Gallery('gallery', 'mediabox', json, 5, true);
}

var mediabox = new tabbox('mediabox');
mediabox.clear = false;
mediabox.onComplete = galleryComplete;




